package com.java.dormitorymanager.mvc.handler;

import com.java.dormitorymanager.pojo.Student;
import com.java.dormitorymanager.service.IStudentService;
import com.java.dormitorymanager.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 学生的逻辑代码
 * @author: ShuangChenYue
 * @create: 2021-05-16 17:13
 **/
public class StudentHandler
{
    IStudentService studentService = new StudentServiceImpl();
//======================================================学生登录=========================================================
    public void studentLogin(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过学生登录按钮来到此方法");
            req.setAttribute("alert","学生禁止登录！");
            req.getRequestDispatcher("/ViewExceptionPage").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================学生注册=========================================================
    public void studentRegister(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过学生注册按钮来到此方法");
            //在此方法中获取注册界面的值
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String stunum = req.getParameter("stunum");
            String studentAge = req.getParameter("age");
            int age = Integer.parseInt(studentAge);
            String gender = req.getParameter("gender");
            String email = req.getParameter("email");
            String status = req.getParameter("status");
            //将其封装在学生类中
            Student student = new Student();
            student.setName(name);
            student.setPassword(password);
            student.setStunum(stunum);
            student.setAge(age);
            student.setGender(gender);
            student.setEmail(email);
            student.setStatus(status);
            //调用studentService.studentRegister(Student student)即可
            studentService.studentRegister(student);
            //注册完成请求转发至登录界面
            req.getRequestDispatcher("/html/student/login.html").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=============================================进入管理学生界面前的数据准备================================================
    public void manageStudentManager(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("这是进入学生管理页面前的数据查询准备");
            //这里查找所有男同学和女同学
            List<Student> allMaleStudentsList = studentService.findAllMaleStudents();
            req.setAttribute("allMaleStudentsList",allMaleStudentsList);

            List<Student> allFemaleStudentsList = studentService.findAllFemaleStudents();
            req.setAttribute("allFemaleStudentsList",allFemaleStudentsList);
            //存好之后将请求转发到管理学生界面
            req.getRequestDispatcher("/ViewManageStudent").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=========================================进入显示查询学生结果的界面前的数据准备===========================================
    public void searchStudentByName(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("已进入显示查询学生结果前的数据准备方法中");
            //获得name
            String name = req.getParameter("name");
            //调用studentService.findStudentByName(String name)方法并返回一个Student对象
            Student student = studentService.findStudentByName(name);
            req.setAttribute("student",student);
            req.getRequestDispatcher("/ViewSearchStudentByName").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=============================================显示学生详情前的数据准备方法================================================
    public void showStudentDetail(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击详情后进入该方法");
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            Student student = studentService.findStudentById(id);
            req.setAttribute("student",student);
            req.getRequestDispatcher("/ViewStudentDetail").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================删除学生==========================================================
    public void deleteStudent(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击删除学生按钮进入此方法");
            //用户点击删除按钮后，会带着一个学生id来此处，获取学生id
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            //将其强转后，调用studentService.deleteStudentById(int id)方法，将id传入，执行删除方法
            studentService.deleteStudentById(id);
            //删除完成后，需要重新进入管理学生界面，于是需要去数据库中重新查询，而后请求转发至管理学生界面
            //这里查找所有男同学和女同学
            List<Student> allMaleStudentsList = studentService.findAllMaleStudents();
            req.setAttribute("allMaleStudentsList",allMaleStudentsList);

            List<Student> allFemaleStudentsList = studentService.findAllFemaleStudents();
            req.setAttribute("allFemaleStudentsList",allFemaleStudentsList);
            //存好之后将请求转发到管理学生界面
            req.getRequestDispatcher("/ViewManageStudent").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================添加学生=========================================================
    public void addStudent(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击add按钮后插入数据库");
            //此处应获取输入框的所有值
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String stunum = req.getParameter("stunum");
            String studentAge = req.getParameter("age");
            int age = Integer.parseInt(studentAge);
            String gender = req.getParameter("gender");
            String status = req.getParameter("status");
            String email = req.getParameter("email");

            Student student = new Student();
            student.setName(name);
            student.setPassword(password);
            student.setStunum(stunum);
            student.setAge(age);
            student.setGender(gender);
            student.setStatus(status);
            student.setEmail(email);
            //调用studentService.studentRegister(Student student)方法
            studentService.studentRegister(student);
            //重新于数据库中查询数据并返回集合后再次进入显示界面
            //这里查找所有男同学和女同学
            List<Student> allMaleStudentsList = studentService.findAllMaleStudents();
            req.setAttribute("allMaleStudentsList",allMaleStudentsList);

            List<Student> allFemaleStudentsList = studentService.findAllFemaleStudents();
            req.setAttribute("allFemaleStudentsList",allFemaleStudentsList);
            //存好之后将请求转发到管理学生界面
            req.getRequestDispatcher("/ViewManageStudent").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================修改学生信息========================================================
    public void toUpdateStudent(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            //该方法是进入修改学生界面前的数据准备
            System.out.println("通过修改按钮来到此方法");
            //1.当用户点击修改按钮时，会带着一个id来到此方法，我们在此获取id，并将其强转
            String studentId = req.getParameter("id");
            int id = Integer.parseInt(studentId);
            //2.强转后，调用studentService.findStudentById(int id)方法，返回一个student对象
            Student student = studentService.findStudentById(id);
            //3.将其存放在req里，并转发给下一个页面即可
            req.setAttribute("student",student);
            //4.转发至显示页面
            req.getRequestDispatcher("/ViewUpdateStudent").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================修改学生信息后========================================================
    //当用户点击Update按钮后，会请求转发到该方法中
    public void updateStudent(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过Update按钮进入此方法");
            //1.首先获取修改页面的所有的值
            String studentId = req.getParameter("id");
            int id = Integer.parseInt(studentId);
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String stunum = req.getParameter("stunum");
            String studentAge = req.getParameter("age");
            int age = Integer.parseInt(studentAge);
            String gender = req.getParameter("gender");
            String email = req.getParameter("email");
            String status = req.getParameter("status");
            String dornum = req.getParameter("dornum");
            //2.将其封装在对象中
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setPassword(password);
            student.setStunum(stunum);
            student.setAge(age);
            student.setGender(gender);
            student.setEmail(email);
            student.setStatus(status);
            student.setDornum(dornum);
            //3.调用studentService.updateStudent(Student student)方法，将对象传入并更新
            studentService.updateStudent(student);
            //4.更新完成后，重新查询数据库中所有学生信息，而后请求转发至显示页面
            //这里查找所有男同学和女同学
            List<Student> allMaleStudentsList = studentService.findAllMaleStudents();
            req.setAttribute("allMaleStudentsList",allMaleStudentsList);

            List<Student> allFemaleStudentsList = studentService.findAllFemaleStudents();
            req.setAttribute("allFemaleStudentsList",allFemaleStudentsList);
            //存好之后将请求转发到管理学生界面
            req.getRequestDispatcher("/ViewManageStudent").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
